package com.qyhl.webtv.module_news.luckydraw.ticketsinfo;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.LuckDrawInfoBean;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

@Route(path = ARouterPathConstant.LUCK_DRAW_TICKETS)
public class LuckDrawTicketsInfoActivity extends BaseActivity implements LuckDrawTicketsInfoContract.LuckDrawTicketsInfoView {
    @BindView(R2.id.back_btn)
    ImageView backBtn;
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    @Autowired(name = "list")
    List<LuckDrawInfoBean> mList;

    private CommonAdapter commonAdapter;

    private String lastId;
    @Autowired(name = "phone")
    String phoneNum;

    private LuckDrawTicketsInfoPresenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.news_activity_luck_draw_winner;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new LuckDrawTicketsInfoPresenter(this);
        phoneNum = getIntent().getStringExtra("phone");
        if (mList == null) {
            mList = new ArrayList<>();
            lastId = "0";
        } else {
            lastId = mList.get(mList.size() - 1).getId() + "";
        }
        loadMask.setStatus(LoadingLayout.Loading);
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
    }


    private void initView() {
        title.setText("我的发票");

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        commonAdapter = new CommonAdapter<LuckDrawInfoBean>(this, R.layout.news_item_luck_draw_tickets, mList) {

            @Override
            protected void convert(ViewHolder viewHolder, LuckDrawInfoBean bean, int i) {
                TextView title = viewHolder.getView(R.id.title);
                title.setText(bean.getActivity());

                TextView invoiceCode = viewHolder.getView(R.id.code);
                invoiceCode.setText("发票代码： " + bean.getReceiptCode());

                TextView invoiceNum = viewHolder.getView(R.id.num);
                invoiceNum.setText("发票号码： " + bean.getReceiptCodeNum());

                TextView invoiceDate = viewHolder.getView(R.id.date);
                invoiceDate.setText("开票日期： " + DateUtils.dateToString(bean.getReceiptDate(), DateUtils.date_ymd));
            }
        };


        recycleView.setAdapter(commonAdapter);
        loadMask.setStatus(LoadingLayout.Success);

    }

    private void setListeners() {
        //返回
        backBtn.setOnClickListener(view -> finish());

        //刷新
        refresh.setOnRefreshListener(refreshLayout -> {
            lastId = "0";
            mPresenter.queryTicketsInfo(phoneNum, lastId);
        });

        //加载更多
        refresh.setOnLoadMoreListener(refreshLayout -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.queryTicketsInfo(phoneNum, lastId);
        });

        //重载
        loadMask.setOnReloadListener(view -> {
            lastId = "0";
            mPresenter.queryTicketsInfo(phoneNum, lastId);
        });
    }

    @Override
    public void queryTicketsInfoSuccess(List<LuckDrawInfoBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        if (isMore) {
            refresh.finishLoadMore();
            mList.addAll(beanList);
        } else {
            refresh.finishRefresh();
            mList.clear();
            mList.addAll(beanList);
        }
        lastId = beanList.get(beanList.size() - 1).getId() + "";
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void queryTicketsInfoError(String msg) {
        refresh.finishRefresh();
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
    }

    @Override
    public void setEmpty(String msg) {
        refresh.finishRefresh();
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);
        loadMask.setReloadButtonText("点击重试~");
    }

    @Override
    public void setLog(String msg) {
        refresh.finishLoadMore();
        Log.i("info", msg);
    }

    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~");
    }

    @Override
    public void showToast(String msg) {
        refresh.finishLoadMore();
        Toasty.normal(this, msg).show();
    }


}
